/***************************************************************************
begin                : May 01 2011
copyright            : (C) 2011 by Benoit Valot
email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import val.lemonde.FragmentMain.OnCategorieSelectedListener;

import com.markupartist.android.widget.ActionBar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;

public class ActivityMain extends ActivityLemondeBase implements
		OnCategorieSelectedListener {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		getMenuInflater()
				.inflate(R.menu.categorie_menu_top, actionBar.asMenu());
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setTitle("Actualités");

		// on charge le fragment
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		if (savedInstanceState != null) {
			FragmentMain fragmentOld = (FragmentMain) fragmentManager
					.findFragmentByTag("categories");
			fragmentTransaction.remove(fragmentOld);
		}
		FragmentMain fragment = new FragmentMain();
		if (this.isDualMode())
			fragmentTransaction.add(R.id.container_little, fragment,
					"categories");
		else {
			fragmentTransaction.add(R.id.container, fragment, "categories");
		}
		fragmentTransaction.commit();
	}

	@Override
	public void onCategorieSelected(Categorie cat, int pos) {
		Log.d("lemonde", "Load page :" + cat.getName());
		Intent i = new Intent(this, ActivityListeArticles.class);
		i.putExtra("cat", cat);
		i.putExtra("position", pos);
		this.startActivity(i);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.info_item:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("A propos");
			builder.setMessage(R.string.apropos);
			builder.setPositiveButton("ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			builder.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
